<?php
class fh extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览发货', '1-2-3');
		$da = $this -> pub -> get_setting('发货', 'Delivery');
		$this -> load -> view('adminx/fh', $da);
	}

	function get() {
		$this -> pub -> logp('获取发货信息', '1-2-3');
		if ($this -> input -> post('key')) {
			$this -> db -> like('pan_bill.name', $this -> input -> post('key'));
		}

		$this -> db -> where('thfs', 1);
		$this -> db -> order_by('bill_id', 'asc');
		if ($this -> input -> post('state') > 0) {
			$this -> db -> where('pan_bill.state', $this -> input -> post('state'));
		}
		if ($this -> input -> post('state') == -1) {
			$this -> db -> where('pan_bill.state', 0);
		}
		$this -> db -> select('pan_bill.*,pan_member.nickname,pan_member.headimgurl');
		$this -> db -> join('member', 'pan_member.mid=pan_bill.mid');

		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}
		$this -> db -> limit(300);
		$q = $this -> db -> get_where('bill');
		$da['content'] = '';
		foreach ($q->result_array() as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td><a target="_blank" href="/iadmin/member/info/' . $v['mid'] . '"><img src="' . ($v['headimgurl'] ? $v['headimgurl'] : '/mex/lance/img/tx.jpg') . '" width="60" style="float:left;" /></a></td>';
			$da['content'] .= '<td> ' . $v['name'] . '(' . $v['nickname'] . ')' . '<br /> ' . $v['phone'] . '</td>';
			$da['content'] .= '<td>' . $v['dq'] . ' ' . $v['zz'] . '</td>';

			$this -> db -> select('sp_name,sp_color');
			$q = $this -> db -> get_where('bill_item', array('bill_id' => $v['bill_id']));
			$sp = $q -> result_array();
			$title = '';
			foreach ($sp as $vv) {
				$title .= $vv['sp_name'] . ' ' . $vv['sp_color'] . '<br>';
			}

			$da['content'] .= '<td>' . $title . '</td>';
			$da['content'] .= '<td>' . $v['numb'] . '</td>';
			$da['content'] .= '<td id="wl_name_' . $v['bill_id'] . '">' . $v['wl_name'] . '</td>';
			//spinner
			$da['content'] .= '<td><input onkeydown="if(event.keyCode==13){doup_bill($(this),\'' . $v['bill_id'] . '\');}" onfocus="dofocus($(this))"  type="text" class="form-control set_wl" placeholder="物流订单号" value="' . $v['wl_hao'] . '"></td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

	function bill_up() {
		$this -> pub -> logp('发货 - ' . $this -> input -> post('idx'), '1-2-3');
		ignore_user_abort(true);
		$q = $this -> db -> get_where('bill', array('bill_id' => $this -> input -> post('idx')));
		$bill = $q -> row_array(0);
		$this -> session -> set_userdata('wl_name', $this -> input -> post('wl_name'));
		if ($bill['state'] == 2 or $bill['state'] == 1) {
			if ($this -> input -> post('wl_name')) {
				$da['wl_name'] = $this -> input -> post('wl_name');
			}
			if ($this -> input -> post('wl_hao')) {
				$da['wl_hao'] = $this -> input -> post('wl_hao');
			}
			$da['state'] = 2;
			//设置权限
			if ($this -> session -> userdata('state') == 3) {
				$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
			}
			$this -> db -> update('bill', $da, array('bill_id' => $this -> input -> post('idx')));

			//发送通知
			$this -> db -> where('mid', $bill['mid']);
			$q = $this -> db -> get_where('member');
			$m = $q -> row_array(0);

			$this -> db -> select('sp_name,sp_dl,sp_dl1,sp_dl2,sp_lq,numb,sp_id');
			$q = $this -> db -> get_where('bill_item', array('bill_id' => $this -> input -> post('idx')));
			$sp = $q -> result_array();
			$sp_name = '';
			foreach ($sp as $v) {
				$sp_name .= $v['sp_name'] . ' ';
			}

			$this -> pub -> send_zd_text($m['openid'], '【' . sprintf("%08d", $this -> input -> post('idx')) . '】您购买的<' . $sp_name . '>已发货！' . PHP_EOL . PHP_EOL . '<a href=\'' . $this -> config -> item('base_urlx') . '/shop/bill\'>查询订单</a>');

			echo json_encode(array('type' => 1));
		} else {
			echo json_encode(array('type' => 2));
		}

	}

	function dh($page = 0) {
		$this -> pub -> logp('导出发货信息', '1-2-3');

		header("Content-type: text/html; charset=gbk");
		header("Content-type:application/vnd.ms-excel");
		header("Content-Disposition:attachment; filename=cloudXT_dingdan.xls");

		$this -> db -> where('thfs', 1);
        $this -> db -> where('bill_id >', 11021);
		//$this -> db -> limit(800, $page * 800);
		$this -> db -> order_by('bill_id', 'asc');
		$this -> db -> where('pan_bill.state', 1);

		//设置权限
		if ($this -> session -> userdata('state') == 3) {
			$this -> db -> where('bill_id in(select bill_id from pan_bill_item where sp_id in(select sp_id from pan_sp where staff_id=' . $this -> session -> userdata('sid') . '))');
		}
		//订单号  订单时间  单价  金额  检索的时候 加入时间
		$q = $this -> db -> get_where('bill');
		$da['content'] = "订单号\t";
		$da['content'] .= "供货商\t";
		$da['content'] .= "姓名\t";
		$da['content'] .= "手机\t";
		$da['content'] .= "地址\t";
		$da['content'] .= "货物\t";
		$da['content'] .= "数量\t";
		$da['content'] .= "金额\t";
		$da['content'] .= "订单时间\t";
		$da['content'] .= "\n";

		$da['content'] = $this -> utf82gb($da['content']);
		foreach ($q->result_array() as $v) {
			$q = $this -> db -> get_where('bill_item', array('bill_id' => $v['bill_id']));
			$bill_item = $q -> result_array();
			foreach ($bill_item as $vv) {
				$this -> db -> select('staff_id');
				$q = $this -> db -> get_where('sp', array('sp_id' => $vv['sp_id']));
				$sp = $q -> row_array(0);

				$this -> db -> select('name');
				$q = $this -> db -> get_where('staff', array('staff_id' => $sp['staff_id']));
				$staff = $q -> row_array(0);

				$da['content'] .= $this -> utf82gb($v['bill_id']) . "\t";
				$da['content'] .= $this -> utf82gb($staff['name']) . "\t";
				$da['content'] .= $this -> utf82gb($v['name']) . "\t";
				$da['content'] .= $this -> utf82gb($v['phone']) . "\t";
				$da['content'] .= $this -> utf82gb($v['zz']) . "\t";

				$da['content'] .= $this -> utf82gb($vv['sp_name']) . "\t";
				$da['content'] .= $vv['numb'] . "\t";
				$da['content'] .= $vv['je'] . "\t";
				$da['content'] .= date('Y-m-d H:i:s', $v['mktime']) . "\t";
				$da['content'] .= "\n";

			}

		}
		echo $da['content'];
	}

	function utf82gb($utfstr) {
		$utfstr = stripslashes($utfstr);
		if (function_exists('iconv')) {
			return iconv('utf-8//IGNORE', 'gbk//IGNORE', $utfstr);
		}
		global $UC2GBTABLE;
		$okstr = "";
		if (trim($utfstr) == "") {
			return $utfstr;
		}
		if (empty($UC2GBTABLE)) {
			if (!defined('DS')) {
				define('DS', DIRECTORY_SEPARATOR);
			}
			$filename = FCPATH . APPPATH . 'third_party' . DS . 'gb2312-utf8.dat';
			$fp = fopen($filename, "r");
			while ($l = fgets($fp, 15)) {
				$UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
			}
			fclose($fp);
		}
		$okstr = "";
		$ulen = strlen($utfstr);
		for ($i = 0; $i < $ulen; $i++) {
			$c = $utfstr[$i];
			$cb = decbin(ord($utfstr[$i]));
			if (strlen($cb) == 8) {
				$csize = strpos(decbin(ord($cb)), "0");
				for ($j = 0; $j < $csize; $j++) {
					$i++;
					$c .= $utfstr[$i];
				}
				$c = utf82u($c);
				if (isset($UC2GBTABLE[$c])) {
					$c = dechex($UC2GBTABLE[$c] + 0x8080);
					$okstr .= chr(hexdec($c[0] . $c[1])) . chr(hexdec($c[2] . $c[3]));
				} else {
					$okstr .= "&#" . $c . ";";
				}
			} else {
				$okstr .= $c;
			}
		}
		$okstr = trim($okstr);
		return $okstr;
	}

}
?>